﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using CoreMVVM.Commands;

namespace WpfModelViewApplication2.AppCommands
{

    public static class TextBoxTextChanged
    {
        private static readonly DependencyProperty TextBoxTextChangedBehaviorProperty
            = DependencyProperty.RegisterAttached(
            "TextBoxTextChangedBehavior",
            typeof(TextBoxTextChangedBehavior),
            typeof(TextBoxTextChanged),
            null);

        public static readonly DependencyProperty CommandProperty
            = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(TextBoxTextChanged),
            new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty
            = DependencyProperty.RegisterAttached(
           "CommandParameter",
           typeof(object),
           typeof(TextBoxTextChanged),
           new PropertyMetadata(OnSetCommandParameterCallback));

        public static ICommand GetCommand(TextBox control)
        {
            return control.GetValue(CommandProperty) as ICommand;
        }

        public static void SetCommand(TextBox control, ICommand command)
        {
            control.SetValue(CommandProperty, command);
        }

        public static void SetCommandParameter(TextBox control, object parameter)
        {
            control.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(TextBox control)
        {
            return control.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            TextBox control = dependencyObject as TextBox;
            if (control != null)
            {
                TextBoxTextChangedBehavior behavior = GetOrCreateBehavior(control);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            TextBox control = dependencyObject as TextBox;
            if (control != null)
            {
                TextBoxTextChangedBehavior behavior = GetOrCreateBehavior(control);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static TextBoxTextChangedBehavior GetOrCreateBehavior(TextBox control)
        {
            TextBoxTextChangedBehavior behavior =
                control.GetValue(TextBoxTextChangedBehaviorProperty) as TextBoxTextChangedBehavior;
            if (behavior == null)
            {
                behavior = new TextBoxTextChangedBehavior(control);
                control.SetValue(TextBoxTextChangedBehaviorProperty, behavior);
            }
            return behavior;
        }
    }

    public class TextBoxTextChangedBehavior : CommandBehaviorBase<TextBox>
    {
        public TextBoxTextChangedBehavior(TextBox control)
            : base(control)
        {
            control.TextChanged += OnTextChanged;
        }

        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            CommandParameter = e;
            ExecuteCommand();
        }
    }

}

